﻿using System;
using System.ComponentModel;
using System.Data;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TBABST
    {
       
        #region ==dwBB===========
        public static readonly DependencyProperty dwBBProperty = DependencyProperty.Register("dwBB",
            typeof(DataRow), typeof(TBABST),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwBBChanged));
        public DataRow dwBB
        {
            get
            {
                return (DataRow)GetValue(dwBBProperty);
            }
            set
            {
                SetValue(dwBBProperty, value);
            }
        }
        private static void OndwBBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBABST;
            if (src != null)
            {
                src.dwBB = (DataRow)e.NewValue;
            }
        }
        #endregion

        #region ==AUMLFD===========
        public static readonly DependencyProperty AUMLFDProperty = DependencyProperty.Register("AUMLFD",
            typeof(string), typeof(TBABST),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnAUMLFDChanged));
        public string AUMLFD
        {
            get
            {
                return (string)GetValue(AUMLFDProperty);
            }
            set
            {
                SetValue(AUMLFDProperty, value);
                BBLOAD();
            }
        }
        private static void OnAUMLFDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBABST;
            if (src != null)
            {
                src.AUMLFD = (string)e.NewValue;
            }
        }
        #endregion

        #region ==TLP===========
        public static readonly DependencyProperty TLPProperty = DependencyProperty.Register("TLP",
            typeof(string), typeof(TBABST),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTLPChanged));
        public string TLP
        {
            get
            {
                return (string)GetValue(TLPProperty);
            }
            set
            {
                SetValue(TLPProperty, value);
                if (dwBB != null)
                {
                    dwBB["BMLP"] = value;
                }
            }
        }
        private static void OnTLPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TBABST;
            if (src != null)
            {
                src.TLP = (string)e.NewValue;
            }
        }
        #endregion
        public TBABST()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VBABST();
            this.DataContext = MV;
            MESSAGES += "";
            InitializeComponent();
        }
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwBB = dwMaster;
            if (dwBB == null)
            {
                TLP = "";
            }
            else
            {
                TLP = dwBB["BMLP"].ToString();
            }
        } 
  
        public void BBLOAD()
        {

            if (AUMLFD.Trim().Length == 0)
            {
                TBMain.Clear();
                return;
            }
            dwData = TBMain.NewRow();  
            dwData["BMMANDANT"] = app.Mandant;
            dwData["BMBAUMKRS"] = AUMLFD.Substring(0,2);
            dwData["BMBAUMLFD"] = AUMLFD.Substring(2, 6);
            SResult =TMV.QBABST01(dwData); 
            MergeData(SResult, true);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            dwMaster["BMLP"] = Helpers.Helper.Left(dwMaster["BMLP"].ToString().PadRight(8), 8);
            dwMaster["BMAENDBEN"] = app.User;
            dwMaster["BMAENDDAT"] = MV.date;
            TMV.UpdateBABST(dwMaster, 2);
            base.SaveCommand(sender, e);
        } 
    }
}
